/* eslint-disable no-continue */
/*
 * @Author: liuyonghu
 * @Date: 2022-08-12 17:13:17
 * @LastEditTime: 2023-11-15 10:25:43
 * @LastEditors: liuyonghu
 * @Description: request ulits
 * @FilePath: \as-front\src\requests\utils.ts
 */
var nullVals = ['', null];
function isNull(val) {
    // 删除 {}
    return nullVals.includes(val) || JSON.stringify(val) === '{}';
    // return nullVals.includes(val);
}
function getNullPath(nullKeys, keys, params, firstKey) {
    var _loop_1 = function (index) {
        var key = keys[index];
        var val = params[key];
        var keyPath = firstKey
            ? ''.concat(firstKey, ',').concat(key)
            : ''.concat(key);
        // debugger;
        if (isNull(val)) {
            var keyObj = nullKeys.find(function (it) { return it.key === keyPath; });
            if (!keyObj) {
                nullKeys.push({
                    key: keyPath,
                });
            }
            return "continue";
        }
        if (Object.prototype.toString.call(val) === '[object Object]') {
            var subKeys = Object.keys(val);
            if (subKeys.length > 0) {
                getNullPath(nullKeys, subKeys, val, keyPath);
            }
            else {
                // 删除 {}
                nullKeys.push({
                    key: keyPath,
                });
            }
        }
    };
    for (var index = 0; index < keys.length; index += 1) {
        _loop_1(index);
    }
}
function getAllNullKeys(params) {
    var keys = Object.keys(params);
    var nullKeys = [];
    if (keys.length > 0) {
        getNullPath(nullKeys, keys, params, '');
    }
    return nullKeys;
}
export function getValByPath(params, pathObj) {
    var keys = pathObj.key.split(',');
    var obj = params;
    for (var i = 0; i < keys.length; i += 1) {
        var key = keys[i];
        if (i === keys.length - 1) {
            return obj[key];
        }
        obj = obj[key];
    }
}
function deleteNullByPath(params, pathObj, level) {
    var keys = pathObj.key.split(',');
    var obj = params;
    debugger;
    if (keys.length >= level) {
        for (var i = 0; i < keys.length; i += 1) {
            var key = keys[i];
            if (i === keys.length - 1) {
                delete obj[key];
                continue;
            }
            obj = obj[key];
        }
    }
}
function handleNullByPath(params, pathObj, level) {
    // console.log(pathObj);
    deleteNullByPath(params, pathObj, level);
    var keys = pathObj.key.split(',');
    if (keys.length > 1) {
        keys.pop();
        var nexKey = keys.join(',');
        var val = getValByPath(params, { key: nexKey });
        if (isNull(val)) {
            handleNullByPath(params, { key: nexKey }, level);
        }
    }
}
export function deleteNull(params, level) {
    if (level === void 0) { level = 2; }
    console.log(' deleteNull params = ', JSON.stringify(params, null, '\t'));
    var nullPaths = getAllNullKeys(params);
    console.log(' deleteNull nullPaths = ', JSON.stringify(nullPaths, null, '\t'));
    // debugger;
    for (var index = 0; index < nullPaths.length; index += 1) {
        var keyPath = nullPaths[index];
        handleNullByPath(params, keyPath, level);
    }
}
export default { deleteNull: deleteNull };
